﻿

namespace VMWareLabWorkflowsDataContracts.VMWareLabDetailsDialogControls
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Controls;

    using Microsoft.TeamFoundation.Build.Client;
    using Microsoft.TeamFoundation.Build.Workflow.Activities;

    using VMWareLabWorkflowsDataContracts.VMWareLabDetailsDialogControls.ViewModel;

    /// <summary>
    /// Interaction logic for BuildDetailsControl.xaml
    /// </summary>
    public partial class BuildDetailsControl : UserControl
    {
        public BuildDetailsViewModel BuildDetailsViewModel { get; set; }
        public BuildDetailsControl()
        {
            InitializeComponent();
            this.BuildDetailsViewModel = new BuildDetailsViewModel();
            this.DataContext = this.BuildDetailsViewModel;
        }

        /// <summary>
        /// Handles the SelectionChanged event of the cbBuildDefinitions control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.SelectionChangedEventArgs"/> instance containing the event data.</param>
        private void cbBuildDefinitions_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            IBuildDefinition bd = (IBuildDefinition)cbBuildDefinitions.SelectedItem;
            if (this.BuildDetailsViewModel.BuildDetailsVm.BuildDefinitionUri != bd.Uri)
            {
                this.BuildDetailsViewModel.BuildDetailsVm.BuildDefinitionUri = bd.Uri;
                this.BuildDetailsViewModel.BuildDetailsVm.BuildDefinitionName = bd.Name;
                this.BuildDetailsViewModel.GetBuildDetails(bd.TeamProject, bd.Name);
                this.BuildDetailsViewModel.GetBuildSettings();
            }
        }

        /// <summary>
        /// Handles the SelectionChanged event of the cbExistingBuilds control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.SelectionChangedEventArgs"/> instance containing the event data.</param>
        private void cbExistingBuilds_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbExistingBuilds.SelectedItem != null)
            {
                var bd = (KeyValuePair<string, Uri>)cbExistingBuilds.SelectedItem;
                this.BuildDetailsViewModel.BuildDetailsVm.BuildUri = bd.Value;
            }
        }

        /// <summary>
        /// Handles the SelectionChanged event of the cbPlatformConfiguration control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.SelectionChangedEventArgs"/> instance containing the event data.</param>
        private void cbPlatformConfiguration_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbPlatformConfiguration.SelectedItem != null)
            {
                PlatformConfiguration pc = (PlatformConfiguration)cbPlatformConfiguration.SelectedItem;
                this.BuildDetailsViewModel.BuildDetailsVm.Configuration = pc;
            }
        }
    }
}
